package com.yongchao.role.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yongchao.base.BaseController;
import com.yongchao.role.bean.RoleBean;
import com.yongchao.role.service.Serviceimp.YYC_RoleServiceImp;
import com.yongchao.role.service.YYC_RoleService;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;


@WebServlet(name="YYC_GetRoleController",urlPatterns="/yyc/find")
public class YYC_GetRoleController extends BaseController {

    private YYC_RoleService YYCRoleService =new YYC_RoleServiceImp();


    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //此处method用的事get方法获取
        String method = request.getParameter("method");


      if ("checkRole".equals(method)) {
          checkRole(request,response);

      }else if ("findRoleByPhone".equals(method)) {
          findRoleByPhone(request, response);

      }else if ("findUserCountByRoleId".equals(method)){
          findUserCountByRoleId(request,response);

      }
    }



    private  void checkRole(ServletRequest request,ServletResponse response) throws IOException {


        List<RoleBean> roleList;
        roleList= YYCRoleService.checkRole();
        boolean check=(roleList==null||roleList.size()==0)?false:true;
        printData(check,roleList,404,response);

    }

    private  void findRoleByPhone(ServletRequest request,ServletResponse response) throws IOException {
        List<RoleBean> roleList;

      //  Integer phone = Integer.parseInt(request.getParameter("phone"));
        String phone = request.getParameter("phone");
        roleList= YYCRoleService.findRoleByPhone(phone);

        boolean check=(roleList==null||roleList.size()==0)?false:true;
        printData(check,roleList,404,response);

    }

    private  void findUserCountByRoleId(ServletRequest request,ServletResponse response) throws IOException {
        //获取role_id的Json字符串
        String roleId = request.getParameter("id");
        //将Json字符串转成int类型
        Integer id = Integer.parseInt(roleId);

        Integer count = YYCRoleService.findUserCountByRoleId(id);
        printData(true,count,200,response);

    }





}


